package cn.cs.controller;

import cn.cs.common.*;
import cn.cs.exception.UserErrorCode;
import cn.cs.pojo.CsUser;
import cn.cs.pojo.RepairUser;
import cn.cs.service.RepairUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/repUser")
public class RepairUserController {

    @Resource
    private RepairUserService service;
    @Resource
    private TokenUtils tokenUtils;
    /**
     * 账号密码登录
     *
     * @param  request
     * @return 登录结果，登录成功时包含用户信息和token信息
     * @throws Exception
     */
    @RequestMapping(value = "/p/doLogin", method = RequestMethod.POST, produces = {"application/json"})
    @ResponseBody
    public Dto doLogin(HttpServletRequest request) throws Exception {
        return  service.doLogin(request);
    }

    /****
     * 注册接口
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/p/register")
    @ResponseBody
    public Dto register(HttpServletRequest request,  HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.register(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("注册失败","002");
        }
        return dto;
    }

    /****
     * 修改维修工个人信息
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/updateRepUser")
    @ResponseBody
    public Dto updateRepUser(HttpServletRequest request,  HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.updateRepUser(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("注册失败","002");
        }
        return dto;
    }

    /****
     * 验证手机号是否已注册
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/checkPhone")
    @ResponseBody
    public Dto checkPhone(HttpServletRequest request,  HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.checkPhone(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("注册失败","002");
        }
        return dto;
    }


    /****
     *服务类型范围接口
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/showFaultType")
    @ResponseBody
    public Dto showFaultType(HttpServletRequest request,  HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.showFaultType(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","002");
        }
        return dto;
    }

    /****
     * 查看个人信息
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/getMyInfo")
    @ResponseBody
    public Dto getMyInfo(HttpServletRequest request,  HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.getMyInfo(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","002");
        }
        return dto;
    }

    /****
     * 开始结束接单
     * @param request
     * @param
     * * @return
     */
    @RequestMapping("/changeWorkState")
    @ResponseBody
    public Dto changeWorkState(HttpServletRequest request,  HttpServletResponse response){
        String state = request.getParameter("state");
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.changeWorkState(request,state);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("更改失败","002");
        }
        return dto;
    }


    /**
     * 用户注销
     * 参数需要放在header中，将token传进去
     * */
    @RequestMapping(value = "/logout", method = RequestMethod.POST, produces = "application/json", headers = "token")
    @ResponseBody
    public Dto logout(HttpServletRequest request) throws Exception{
        String token = request.getHeader("token");
        service.changeWorkState(request,"0");
        tokenUtils.delete(token);
        return  DtoUtil.returnSuccess();
    }

    /****
     * 更新用户手机号
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/updateRepUserPhone")
    @ResponseBody
    public Dto updateRepUserPhone(HttpServletRequest request,  HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.updateRepUserPhone(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("更改失败","002");
        }
        return dto;
    }

    /****
     * 查看私人客户接口
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/getMypriLci")
    @ResponseBody
    public Dto getMypriLci(HttpServletRequest request,  HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.getMypriLci(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","002");
        }
        return dto;
    }
    /****
     *投诉列表接口
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/myComplaint")
    @ResponseBody
    public Dto myComplaint(HttpServletRequest request,  HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.myComplaint(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","002");
        }
        return dto;
    }

    /****
     *投诉详情接口
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/complaintInfo")
    @ResponseBody
    public Dto complaintInfo(HttpServletRequest request,  HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.complaintInfo(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","002");
        }
        return dto;
    }

    /****
     * 修改证件照
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/updateCertificateImgs")
    @ResponseBody
    public Dto updateCertificateImgs(HttpServletRequest request,  HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.updateCertificateImgs(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("更改失败","002");
        }
        return dto;
    }

    /****
     * 查看平台政策接口
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/getPolicy")
    @ResponseBody
    public Dto getPolicy(HttpServletRequest request,  HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.getPolicy(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","002");
        }
        return dto;
    }
    /****
     * 查看我的每日收益
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/getMyEarnings")
    @ResponseBody
    public Dto getMyEarnings(HttpServletRequest request,  HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.getMyEarnings(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","002");
        }
        return dto;
    }

    /****
     * 查看我的收益统计
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/getCountMyEarnings")
    @ResponseBody
    public Dto getCountMyEarnings(HttpServletRequest request,  HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.getCountMyEarnings(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","002");
        }
        return dto;
    }

    /****
     * 申请结算接口
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/appSettlement")
    @ResponseBody
    public Dto appSettlement(HttpServletRequest request,  HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.appSettlement(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","002");
        }
        return dto;
    }

    /****
     * 上传头像接口
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/uploadImage")
    @ResponseBody
    public Dto uploadImage(HttpServletRequest request,  HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.uploadImage(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","002");
        }
        return dto;
    }



    /****
     * 可结算金额
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/appSetMoney")
    @ResponseBody
    public Dto appSetMoney(HttpServletRequest request,  HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.appSetMoney(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","002");
        }
        return dto;
    }


    /**
     * 分页查询 维修工分类
     * @param pageNumber
     * @param pageSize
     * @return
     * @throws Exception
     * TODO 添加搜索
     */
    @RequestMapping(value = "/csPlatform/getRepairUserList")
    @ResponseBody
    public Dto getRepairUserList(Integer pageNumber, Integer pageSize, String phone, String area, String invitation_code, String type, String province, String city)throws Exception{
        type = EmptyUtils.isEmpty(type)?"0":type;
        Map<String, Object> param = new HashMap<>();
        param.put("phone", phone);
        param.put("province", province);
        param.put("city", city);
        param.put("area", area);
        param.put("invitation_code", invitation_code);
        param.put("type", type);
        return DtoUtil.returnSuccess("获取成功", service.queryRepairUserPageByMap(param, pageNumber, pageSize));
    }

    /**
     * 根据维修工id查询维修工维修区域
     * @param id
     * @return
     */
    @RequestMapping(value = "/p/getRepairUserArea")
    @ResponseBody
    public Dto getRepairUserArea(String id){
        return DtoUtil.returnSuccess("获取成功", service.getRepairUserArea(id));
    }



    /**
     * 修改维修工状态
     * @param id
     * @param validState 有效状态 1生效 0失效
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/updateUserState")
    @ResponseBody
    public Dto updateUserState(String id, String validState)throws Exception{
        RepairUser repairUser = new RepairUser();
        repairUser.setId(id);
        repairUser.setValidState(validState);
        service.modifyRepairUser(repairUser);
        return DtoUtil.returnSuccess("修改成功", "");
    }


    /****
     * 注册接口 微信注册
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/p/registerWx")
    @ResponseBody
    public Dto registerWx(HttpServletRequest request,  HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=service.registerWx(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("注册失败","002");
        }
        return dto;
    }



    /**
     * 根据产品分类查询技能证件设置
     * @param fault_id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/getCertificateListByFaultId")
    @ResponseBody
    public Dto getCertificateListByFaultId(String fault_id)throws Exception {
        return DtoUtil.returnSuccess("获取成功", service.getCertificateListByFaultId(fault_id));
    }


    /**
     * 保存技能证件设置
     * @param fault_id
     * @param type
     * @return
     */
    @RequestMapping(value = "/csPlatform/saveCertificateListByFaultId")
    @ResponseBody
    public Dto saveCertificateListByFaultId(String fault_id, String type){
        Map<String, Object> map = new HashMap<>();
        map.put("fault_id", fault_id);
        map.put("certificates_type", type);
        map.put("id", IdWorker.getId());
        try{
            return DtoUtil.returnSuccess("操作成功", service.insertCertificateList(map));
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("操作失败，请检查此类型下是否存在相同的证件类型", "001");
        }
    }

    /**
     * 删除技能证件设置
     * @param ids
     * @return
     */
    @RequestMapping(value = "/csPlatform/delCertificateImg")
    @ResponseBody
    public Dto delCertificateImg(String ids){
        int i = 0;
        for(String id:ids.split(",")){
            i += service.delCertificateImg(id);
        }
        if(i>0){
            return DtoUtil.returnSuccess("操作成功", i);
        }else {
            return DtoUtil.returnFail("操作失败，请重试", "001");
        }
    }

    /**
     * 根据维修工id查询维修工操作证件照片
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/p/getCertificateImgByUserId")
    @ResponseBody
    public Dto getCertificateImgByUserId(String id)throws Exception{
        return DtoUtil.returnSuccess("获取成功", service.getCertificateImgByUserId(id));
    }


    /**
     * 查询某一天接单的维修工
     * @param pageNumber
     * @param pageSize
     * @param createDate
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/csPlatform/getRepairUserDayOrderList")
    @ResponseBody
    public Dto getRepairUserDayOrderList(Integer pageNumber, Integer pageSize, String createDate)throws Exception{
        String newDate = DateUtil.format(new Date(), "yyyy-MM-dd");
        createDate = EmptyUtils.isEmpty(createDate)?newDate:createDate;
        Map<String, Object> param = new HashMap<>();
        param.put("createDate", createDate);
        return DtoUtil.returnSuccess("获取成功", service.getRepairUserDayByMap(param, pageNumber, pageSize));
    }

}
